package com.sdry.web.controller.jyy;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.jyy.Plan;
import com.sdry.model.jyy.PlanDetail;
import com.sdry.model.jyy.PlanExample;
import com.sdry.model.jyy.PlanExample.Criteria;
import com.sdry.service.jyy.PlanDetailServise;
import com.sdry.service.jyy.PlanServise;
import com.sdry.utils.ExcelUtils;
import com.sdry.utils.ResponseUtil;
/**
 * 
 * @ClassName:    PlanController   
 * @Description:  生产计划
 * @Author:       jyy
 * @CreateDate:   2018年9月25日 下午2:45:44   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("plan")
public class PlanController {
	
	@Resource
	private PlanServise planServise;
	@Resource
	private PlanDetailServise planDetailServise;
	
	/**
	 * 进入生产计划页面
	 * @return
	 */
	@RequestMapping("plan")
	public String enterConsole(){
		return "/jyy/plan";
	}
	
	/**
	 * 导入生产计划
	 * @param filename
	 * @param response
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	//@RequestMapping("uploadPlan")
	@RequestMapping(value = "/uploadPlan", method = RequestMethod.POST)
	@ResponseBody
	public void uploadPlan(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {
		 String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/"+file.getOriginalFilename());
		 File newFile=new File(savePath);
		 
		   //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
	       file.transferTo(newFile);
	       
	       //取文件后缀
	       String subfix = savePath.lastIndexOf(".")==-1? "" : savePath.substring(savePath.lastIndexOf(".")+1);
	       System.out.println(subfix);
	       List<List<String>> lists = new ArrayList<>();
	      
	       //获取导入行数
	       List<String> names = names();
	       int num = names.size();
	       if(subfix.equals("xls")){
	    	   lists = ExcelUtils.readXls(savePath, request,num);
	 	   }else if(subfix.equals("xlsx")){
	 		   lists = ExcelUtils.readXlsx(savePath, request,num);
	 	   }else{
	 	   		response.getWriter().print("0");
	 	   }
	       
	      int flag = 1; 
	      /* for (int i = 0; i < lists.size(); i++) {
	    	   List<String> list = new ArrayList<>();
	    	   list= lists.get(i);
	    
	    	   Plan plan =new Plan();
	    	   plan.setPlannumber(list.get(0));
	    	   plan.setDepartment(list.get(1));
	    	   plan.setMtime(list.get(2));
	    	
	    	   int insert = planServise.insert(plan);
	    	   if (insert == 0) {
	    		   flag = 0;
	    		   break;
	    	   }
	       }*/
	      
	      //导入生产计划单信息（第二行）
	      	Plan plan =new Plan();
	   		plan.setPlannumber(lists.get(0).get(0));
	   		plan.setDepartment(lists.get(0).get(1));
	   		plan.setMtime(lists.get(0).get(2));
	   		
	   		Long insert = planServise.insert2(plan);
	   		//导入明细（第四行开始）
	   		for (int i = 2; i < lists.size(); i++) {
		    	   List<String> list = new ArrayList<>();
		    	   list= lists.get(i);
		    
		    	   PlanDetail planDetail  =new PlanDetail();
		    	   planDetail.setRemark1(list.get(0));
		    	   planDetail.setRemark2(list.get(1));
		    	   planDetail.setRemark3(list.get(2));
		    	   planDetail.setDtate(list.get(3));
		    	   planDetail.setStarttime(list.get(4));
		    	   planDetail.setEndtime(list.get(5));
		    	   planDetail.setUnit(list.get(6));
		    	   planDetail.setNum(list.get(7));
		    	   planDetail.setChek(list.get(8));
		    	   planDetail.setRemark(list.get(9));
		    	   
		    	   planDetail.setRemark4(plan.getId()+"");
		    	   
		    	   flag = planDetailServise.insert(planDetail);
		    	   
		    	   planDetail = null;
		       }
	      
	       response.getWriter().print(flag);
	    }
	
	
	/**
	 * 查询生产计划列表
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("planList")
	public void planList(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "limit",required = false)int limit,
            @RequestParam(value = "page",required = false)int page,
            Plan plan) throws Exception{
		PageHelper.startPage(page, limit);
    	
		PlanExample example = new PlanExample();
		Criteria createCriteria = example.createCriteria();
		
		//制单日期
    	if(plan.getMtime() != null && plan.getMtime()  != "") {
    		createCriteria.andMtimeEqualTo(plan.getMtime());
    	}
    	
    	//生产订单号
    	if(plan.getPlannumber()!= null && plan.getPlannumber() != "") {
    		createCriteria.andPlannumberLike("%"+plan.getPlannumber()+"%");
    	}
    	
		List<Plan> list = planServise.selectByExample(example);
		
		PageInfo<Plan> info = new PageInfo<Plan>(list);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", info.getTotal());
        jsonObject.put("data", info.getList());
        ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 导入模板的表头
	 * @return
	 */
	public List<String> names(){
		List<String> list = new ArrayList<>();
		list.add("生产订单号");
		list.add("部门");
		list.add("制单日期");
		
		/*list.add("状态");
		list.add("开始日期");
		list.add("结束日期");
		list.add("单位");
		list.add("数量");
		list.add("质检");
		list.add("备注");*/
		return list;
	}
	
	/**
	 * 安排设备
	 * @param response 响应
	 * @param planDetail 计划详情
	 * @throws Exception 
	 */
	@RequestMapping("/editRemark5")
	public void editRemark5(HttpServletResponse response, PlanDetail planDetail) throws Exception {
		int affact = planDetailServise.updateByPrimaryKeySelective(planDetail);
		ResponseUtil.write(response, affact);
	}
	
}
